// import Router from '../../router/index'
import * as URL from '../../api/cf_index_api'
import http from '../../http/http'
const state = {
  username: sessionStorage.getItem('username')||'',
  level:sessionStorage.getItem('level')|| '',
  token: sessionStorage.getItem('token')||'',
  imgSrc: sessionStorage.getItem('imgsrc')||'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=2043830891,3717215658&fm=26&gp=0.jpg',
  
}
const actions = {
  loginAC ({ commit }, obj) {
    return http({
          url: URL.LOGIN,
          method: 'post',
          data: obj
        }).then(res => {
          // console.log('返回的完整数据',res)
      //得到响应后,
      if (res.data.code===200) {
        //请求成功 将数据写到本地
        // console.log('200',res.data.user.username, res.data.user.level )
        sessionStorage.setItem('username',res.data.user.username)
        sessionStorage.setItem('level',res.data.user.level)
        sessionStorage.setItem('imgsrc',res.data.user.imgSrc)
        //请求成功 将等级和姓名存到 vuex 当中
        commit('loginMU',res.data)
      }
      return res.data
    }).catch(err => {
      console.log('响应出错',err)
    })
    // Router.push('/index')
  },
   testAc(){
    console.log('test()')
  }

}
const mutations = {
  loginMU(state, data) {
    console.log('inMutation',state,data)
    state.userLoginstate = true
    state.username=data.user.username
    state.level=data.user.level
    state.imgSrc=data.user.imgSrc
  }

}
const  getters= {
  levelGE() {
    return state.level==='admin'? '管理员':'普通用户'
  }
}
export default {
    state,
    actions,
    mutations,
    getters
}